/*
 * SoftwareSAR - 1.0
 */
package br.com.cpqd.ssar.mediador.gui;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

/**
 * Janela Criar Novo Projeto
 * @author  Rafael Buck
 * @version 1.0
 */
public class JanelaCriarNovoProjeto extends javax.swing.JDialog {

    /**
     * Controlador de Janelas
     */
    private ControladorJanelas controladorJanelas;
    /**
     * Guarda o Caminho do Projeto
     */
    private String caminhoProjeto = null;

    /**
     * Método Construtor
     * @param controladorJanelas ControladorJanelas
     * @param parent Frame
     * @param modal boolean
     */
    public JanelaCriarNovoProjeto(ControladorJanelas controladorJanelas, java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.controladorJanelas = controladorJanelas;
        // Seta icone padrao
        Image img = Toolkit.getDefaultToolkit().getImage(controladorJanelas.obterCaminhoIcones());
        this.setIconImage(img);

        // Seta como Caminho Inicial o Caminho do Aplicativo
        jTextFieldCaminhoProjeto.setText(System.getProperty("user.dir") + File.separator + "MeusProjetos");
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jRadioButtonMetros = new javax.swing.JRadioButton();
        jRadioButtonCentimetros = new javax.swing.JRadioButton();
        jRadioButtonMilimetros = new javax.swing.JRadioButton();
        jPanel1 = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        jButtonCancelar = new javax.swing.JButton();
        jButtonOK = new javax.swing.JButton();
        projeto = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldCaminhoProjeto = new javax.swing.JTextField();
        jButtonProcurarCaminho = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jTextFieldNomeProjeto = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Novo Projeto");
        setLocationByPlatform(true);
        setResizable(false);

        jPanel2.setBackground(new java.awt.Color(231, 240, 250));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Unidade Dimensional Básica"));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/cpqd/ssar/mediador/gui/recursos/cubo2.png"))); // NOI18N

        jLabel2.setText("Unit Cube  3D : 1[u] x 1[u] x 1[u]");

        jLabel3.setText("Basic Unit [u] in:");

        jRadioButtonMetros.setBackground(new java.awt.Color(231, 240, 250));
        jRadioButtonMetros.setText("meters [m]");
        jRadioButtonMetros.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jRadioButtonMetrosItemStateChanged(evt);
            }
        });
        jRadioButtonMetros.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMetrosActionPerformed(evt);
            }
        });

        jRadioButtonCentimetros.setBackground(new java.awt.Color(231, 240, 250));
        jRadioButtonCentimetros.setText("centimetros [cm]");
        jRadioButtonCentimetros.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jRadioButtonCentimetrosItemStateChanged(evt);
            }
        });
        jRadioButtonCentimetros.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonCentimetrosActionPerformed(evt);
            }
        });

        jRadioButtonMilimetros.setBackground(new java.awt.Color(231, 240, 250));
        jRadioButtonMilimetros.setSelected(true);
        jRadioButtonMilimetros.setText("milimetros [mm]");
        jRadioButtonMilimetros.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jRadioButtonMilimetrosItemStateChanged(evt);
            }
        });
        jRadioButtonMilimetros.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMilimetrosActionPerformed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(231, 240, 250));

        jButtonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/cpqd/ssar/gerenciador/gui/recursos/Cancelar.png"))); // NOI18N
        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        jButtonOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/cpqd/ssar/gerenciador/gui/recursos/confirmar.png"))); // NOI18N
        jButtonOK.setText("Confirmar");
        jButtonOK.setPreferredSize(new java.awt.Dimension(75, 23));
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 528, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButtonOK, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonCancelar)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonCancelar)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonOK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jRadioButtonMetros)
                                .addGap(4, 4, 4)
                                .addComponent(jRadioButtonCentimetros)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jRadioButtonMilimetros)))
                        .addContainerGap())
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jRadioButtonMetros)
                            .addComponent(jRadioButtonCentimetros)
                            .addComponent(jRadioButtonMilimetros))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        projeto.setBackground(new java.awt.Color(231, 240, 250));
        projeto.setBorder(javax.swing.BorderFactory.createTitledBorder("Projeto"));

        jLabel4.setText("Path: ");

        jButtonProcurarCaminho.setText("Search...");
        jButtonProcurarCaminho.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonProcurarCaminhoActionPerformed(evt);
            }
        });

        jLabel6.setText("Nome projeto:");

        jTextFieldNomeProjeto.setText("NameProject");

        javax.swing.GroupLayout projetoLayout = new javax.swing.GroupLayout(projeto);
        projeto.setLayout(projetoLayout);
        projetoLayout.setHorizontalGroup(
            projetoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(projetoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(projetoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(projetoLayout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldCaminhoProjeto, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonProcurarCaminho))
                    .addGroup(projetoLayout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldNomeProjeto, javax.swing.GroupLayout.DEFAULT_SIZE, 465, Short.MAX_VALUE)))
                .addContainerGap())
        );
        projetoLayout.setVerticalGroup(
            projetoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(projetoLayout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(projetoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTextFieldNomeProjeto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(projetoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldCaminhoProjeto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jButtonProcurarCaminho))
                .addContainerGap(67, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(projeto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(projeto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jRadioButtonMetrosItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jRadioButtonMetrosItemStateChanged
// Faz Exclusão Mútua entre Opções
    jRadioButtonMilimetros.setSelected(false);
    jRadioButtonCentimetros.setSelected(false);
}//GEN-LAST:event_jRadioButtonMetrosItemStateChanged

private void jRadioButtonMetrosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMetrosActionPerformed
// Impede Ausência de Seleção
    if (jRadioButtonMetros.isSelected() == false) {
        jRadioButtonMetros.setSelected(true);
    }
}//GEN-LAST:event_jRadioButtonMetrosActionPerformed

private void jRadioButtonCentimetrosItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jRadioButtonCentimetrosItemStateChanged
// Faz Exclusão Mútua entre Opções
    jRadioButtonMilimetros.setSelected(false);
    jRadioButtonMetros.setSelected(false);
}//GEN-LAST:event_jRadioButtonCentimetrosItemStateChanged

private void jRadioButtonCentimetrosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonCentimetrosActionPerformed
// Impede Ausência de Seleção
    if (jRadioButtonCentimetros.isSelected() == false) {
        jRadioButtonCentimetros.setSelected(true);
    }
}//GEN-LAST:event_jRadioButtonCentimetrosActionPerformed

private void jRadioButtonMilimetrosItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jRadioButtonMilimetrosItemStateChanged
// Faz Exclusão Mútua entre Opções
    jRadioButtonCentimetros.setSelected(false);
    jRadioButtonMetros.setSelected(false);
}//GEN-LAST:event_jRadioButtonMilimetrosItemStateChanged

private void jRadioButtonMilimetrosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMilimetrosActionPerformed
// Impede Ausência de Seleção
    if (jRadioButtonMilimetros.isSelected() == false) {
        jRadioButtonMilimetros.setSelected(true);
    }
}//GEN-LAST:event_jRadioButtonMilimetrosActionPerformed

private void jButtonProcurarCaminhoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonProcurarCaminhoActionPerformed
// Procura um Caminho no Computador
    JFileChooser jFileChooser = new JFileChooser(System.getProperty("user.dir") + File.separator + "MeusProjetos");
    // Habilita para Obter apenas Diretorios
    jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    // Muda o título da Janela do File Chooser
    jFileChooser.setDialogTitle("Localizar Caminho do Projeto");
    // Lê a Opção do Usuário
    int option = jFileChooser.showOpenDialog(new JFrame());
    // Teste de Seleção do Usuário
    if (option == JFileChooser.APPROVE_OPTION) {
        caminhoProjeto = jFileChooser.getSelectedFile().toString();
//        System.out.println("JanelaNovoProjeto: " + caminhoProjeto);
        jTextFieldCaminhoProjeto.setText(caminhoProjeto);
    } else if (option == JFileChooser.CANCEL_OPTION) {
    }
}//GEN-LAST:event_jButtonProcurarCaminhoActionPerformed

private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
// TODO add your handling code here:
    // Tenta Criar o Caminho
    File f = null;
    File d = null;
    try {
        // Arquivo
        f = new File(jTextFieldCaminhoProjeto.getText() + File.separator + jTextFieldNomeProjeto.getText());
        // Diretório
        d = new File(jTextFieldCaminhoProjeto.getText() + File.separator);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Verifica se a Pasta do Projeto já Existe
    if (!f.exists()) {
        if (d.isDirectory()) {
            // Unidade Dimensional
            String unidade = null;
            // Verifica a Unidade Dimensional Selecionada
            if (jRadioButtonMilimetros.isSelected()) {
                unidade = "mm";
            } else if (jRadioButtonCentimetros.isSelected()) {
                unidade = "cm";
            } else if (jRadioButtonMetros.isSelected()) {
                unidade = "m";
            }
                try {
                    // Cria um Novo Projeto
                    controladorJanelas.novoProjeto(jTextFieldCaminhoProjeto.getText(), jTextFieldNomeProjeto.getText(), unidade);
                } catch (IOException ex) {
                    Logger.getLogger(JanelaCriarNovoProjeto.class.getName()).log(Level.SEVERE, null, ex);
                }
            dispose();
        } else {
            // Arquivo com nome igual
            controladorJanelas.obterServicos().exibirMensagemAlerta("Nome do Projeto e/ou Caminho Inválidos");
        }
    } else {
        // Arquivo com nome igual
        controladorJanelas.obterServicos().exibirMensagemAlerta("Este Nome de Projeto já Existe");
    }
}//GEN-LAST:event_jButtonOKActionPerformed

private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
// TODO add your handling code here:
    this.dispose();
}//GEN-LAST:event_jButtonCancelarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JButton jButtonProcurarCaminho;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton jRadioButtonCentimetros;
    private javax.swing.JRadioButton jRadioButtonMetros;
    private javax.swing.JRadioButton jRadioButtonMilimetros;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextField jTextFieldCaminhoProjeto;
    private javax.swing.JTextField jTextFieldNomeProjeto;
    private javax.swing.JPanel projeto;
    // End of variables declaration//GEN-END:variables
}
